#/usr/bin/env python
# coding=utf-8
#       menu.py      
#       
#       Copyright 2008 Dirk B. <dirkbo@googlemail.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.
import os

class Menu():
	"""The class for the menu"""
	def __init__(self):
		self.submenu = None
		self.items = []
		self.selected = None
		self.maps = []
		
	def addItem(self, item):																								
		self.items.append(item)
		
	def movedown(self):
		if self.selected == None:
			self.selected = -1
		
		if self.selected < len(self.items) - 1:
			self.selected += 1
		else:
			self.selected = 0
		if self.items[self.selected].selectable == 0:
			self.movedown()
			
		
	def moveup(self):
		if self.selected == None:
			self.selected = len(self.items)
			
		if self.selected > 0:
			self.selected -= 1
		else:
			self.selected = len(self.items) - 1
		
		if self.items[self.selected].selectable == 0:
			self.moveup()
	
	def clear(self):
		del self.items
		self.items = []
	
	def mainmenu(self):
		self.clear()
		self.addItem(MenuItem("Neues Spiel", "newgameplayers"))
		self.addItem(MenuItem("Optionen", "options"))
		self.addItem(MenuItem("Highscores", "scores"))
		self.addItem(MenuItem("Credits", "credits"))
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem("Beenden", "quit"))
		self.selected = None
		self.movedown()
		
	def splash(self):
		self.clear()
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem("Start", "mainmenu"))
		self.selected = None
		self.movedown()
		
	def newgameplayers(self):
		self.clear()
		self.addItem(MenuItem("Neues Spiel...", "X"))
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem("Singleplayer", "newgamesingle"))
		self.addItem(MenuItem("Multiplayer", "newgamemulti"))
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem("Beenden", "mainmenu"))
		self.selected = None
		self.movedown()
		
	def newgamesingle(self):
		self.clear()
		self.addItem(MenuItem("Neues Singleplayer Spiel...", "X"))
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem("Punktejagd", "newgamesinglescore"))
		self.addItem(MenuItem("Rennen", "newgamesinglerace"))
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem("Beenden", "mainmenu"))
		self.selected = None
		self.movedown()
		
	def newgamemulti(self):
		self.clear()
		self.addItem(MenuItem("Neues Multiplayer Spiel...", "X"))
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem("Survival", "newgamemultisurvival"))
		self.addItem(MenuItem("Rennen", "newgamemultirace"))
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem("Beenden", "mainmenu"))
		self.selected = None
		self.movedown()
		
	def ingamemenu(self):
		self.clear()
		self.addItem(MenuItem("Pause", "X"))
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem("Zurück", "ingame"))
		self.addItem(MenuItem("Neu starten", "newgame"))
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem("Beenden", "quit"))
		self.selected = None	
		self.movedown()
		
	def options(self):
		self.clear()
		self.addItem(MenuItem("Gamepad", "conf_joys"))
		self.addItem(MenuItem("Online Service", "conf_service"))
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem("Beenden", "mainmenu"))
		self.selected = None	
		self.movedown()
		
	def get_option(self):
		if self.selected != None:
			return self.items[self.selected].option
		else:
			return -1			
		
	def creditsmenu(self):
		self.clear()
		self.addItem(MenuItem("Programming", ""))
		newm = MenuItem("DirkBo", "")
		newm.align = "right"
		self.addItem(newm)
		self.addItem(MenuItem("Graphics", ""))
		newm = MenuItem("DirkBo", "")
		newm.align = "right"
		self.addItem(newm)
		self.addItem(MenuItem("Testing", ""))
		newm = MenuItem("DirkBo", "")
		newm.align = "right"
		self.addItem(newm)
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem("Back", "mainmenu"))
		self.selected = None
		self.movedown()
		
	def oservicemenu(self, uname="", server=""):
		self.clear()
		newm = MenuItem("Online Service konfigurieren", "")
		newm.align = "center"
		self.addItem(newm)
		
		newm = MenuItem("Benutzername:", "")
		newm.align = "center"
		self.addItem(newm)
		
		newm = MenuItem("<" + uname + ">", "conf_username")
		newm.align = "center"
		self.addItem(newm)
		
		newm = MenuItem("Passwort:", "")
		newm.align = "center"
		self.addItem(newm)
		
		newm = MenuItem("********", "conf_pw")
		newm.align = "center"
		self.addItem(newm)
		
		newm = MenuItem("Server:", "")
		newm.align = "center"
		self.addItem(newm)
		
		newm = MenuItem(server, "conf_server")
		newm.align = "center"
		self.addItem(newm)
		
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem("Speichern", "save_service"))	
		self.addItem(MenuItem(" ", ""))
		self.addItem(MenuItem("Back", "mainmenu"))
		self.selected = None
		self.movedown()
		
	def highscoresmenu(self, highscores, scr=-1):
		self.clear()
		c = 0
		newm = MenuItem("Highscores", "")
		newm.align = "center"
		self.addItem(newm)
		for score in highscores.items:
			if c < 10:
				if scr == -1:
					self.addItem(MenuItem(score.name, ""))
				elif scr == int(score.score):
					self.addItem(MenuItem(score.name, "", True))
				else:
					self.addItem(MenuItem(score.name, ""))
				
				## Wenn kein Zeit-Rekord, einfach dazuaddieren
				if int(score.gtyp) != 2:
					if scr == -1:
						newm = MenuItem(str(score.score), "")
						
					elif scr == int(score.score):
						newm = MenuItem(str(score.score), "")
					else :
						newm = MenuItem(str(score.score), "")
					
				else:
					#Ansonsten zu Zeit konvertieren
					help = int(score.score)
					#Minuten berechnen
					timemin = help / 60000
					#verbleibende Millisekunden berechnen
					help -= timemin * 60000
					#sekunden berechnen
					timesec = help / 1000
					#verbleibende Millisekunden berechnen
					help -= timesec * 1000
					#hundertstel berechnen
					timemsc = help
					rec = "";
					if timemin < 10:
						rec += "0"+ str(timemin)
					else:
						rec += str(timemin)
					rec += ":"
					if timesec < 10:
						rec += "0"+ str(timesec)
					else:
						rec += str(timesec)
					rec += "."
					if timemsc < 10:
						rec += "00"+ str(timemsc)
					elif timemsc < 100:
						rec += "0"+ str(timemsc)
					elif timemsc < 1000:
						rec += str(timemsc)
					if scr == -1:
						newm = MenuItem(rec, "")
					elif scr == int(score.score):
						newm = MenuItem(rec, "", True)
					else :
						newm = MenuItem(rec, "")
						
				newm.align = "right"
				self.addItem(newm)
			c += 1
		## 1. Platzierter Online Eintragen
		#newm = MenuItem("Online", "")
		#newm.align = "center"
		#self.addItem(newm)
		#if highscores.oerster != -1:
		#	self.addItem(MenuItem("1. "+highscores.oerster.name, ""))
		#	newm = MenuItem(str(highscores.oerster.score), "replay")
		#	newm.align = "right"
		#	newm.option = highscores.oerster.replay
		#	self.addItem(newm)
		#if highscores.obesser != -1:
		#	self.addItem(MenuItem("   "+highscores.obesser.name, ""))
		#	newm = MenuItem(str(highscores.obesser.score), "replay")
		#	newm.align = "right"
		#	newm.option = highscores.obesser.replay
		#	self.addItem(newm)
		#if highscores.oeigener != -1:
		#	self.addItem(MenuItem("   "+highscores.oeigener.name, ""))
		#	newm = MenuItem(str(highscores.oeigener.score), "")
		#	newm.align = "right"
		#	self.addItem(newm)
		#if highscores.oschlechter != -1:
		#	self.addItem(MenuItem("   "+highscores.oschlechter.name, ""))
		#	newm = MenuItem(str(highscores.oschlechter.score), "replay")
		#	newm.option = highscores.oschlechter.replay
		#	newm.align = "right"
		#	self.addItem(newm)
		
		self.addItem(MenuItem("Back", "mainmenu"))
		self.selected = None
		self.movedown()
		
	def levelsmenu(self):
		startdir = "data/maps"
		self.clear()
		self.addItem(MenuItem("Levelwahl ...", "X"))
		self.maps = []
		flist = os.listdir(startdir)
		for f in flist:
			if f.endswith('.lev') == True:
				self.maps.append(f)
		for f in self.maps:
			## Für den titel .lev abschneiden
			t = f.rpartition('.lev')
			## und _ durch ' ' ersetzen
			Tn = t[0].replace('_', ' ')
			self.addItem(MenuItem(Tn.title(), f))
		self.selected = None
		self.movedown()
			
				
class MenuItem():
	"""Menu items"""
	def __init__(self, caption, changestate, hl=False):
		self.highlight = hl
		self.option = ""
		self.caption = caption
		if changestate == "":
			self.selectable = 0
			self.align = "left"
		elif changestate == "X":
			self.selectable = 0
			self.align = "center"
		else:
			self.selectable = 1
			self.align = "center"
			self.changestate = changestate
